package com.bawei.module_home.ui.merchant

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.AllEventBus
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseFragment
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.adapter.MerchantAdapter
import com.bawei.module_home.databinding.FragmentMerchantABinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.MerchantModelItem
import com.bawei.module_home.ui.merchant.MerchantDetailActivity.MerchantApprovedEvent
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

//import org.greenrobot.eventbus.EventBus
//import org.greenrobot.eventbus.Subscribe
//import org.greenrobot.eventbus.ThreadMode

@AndroidEntryPoint
class MerchantAFragment : BaseFragment<HomeServiceViewModel, FragmentMerchantABinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_merchant_a
    val mViewModel: HomeServiceViewModel by viewModels()

    val adapter: MerchantAdapter by lazy {
        MerchantAdapter()
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onRefreshEvent(event: AllEventBus.RefreshEvent) {
        if (event.success && event.dataType == "user_data") {
            ToastUtils.showLong(context,"刷新成功")
            mViewModel.merchant()
        }
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }


    override fun initView() {
        super.initView()
        initAdapter()

        adapter.setOnItemClickListener(object : MerchantAdapter.OnItemClickListener{
            override fun clickListener(
                item: MerchantModelItem,
                position: Int
            ) {
                ARouter.getInstance().build(Router.MERCHANT_DETAIL_PAGE).withInt("merchantID",item.id).navigation()
            }

        })
    }

    override fun initData() {
        mViewModel.merchant()
    }

    override fun initListener() {
        mViewModel._mMerchantState.observeWithLifecycle(this){
            when(it){
                Empty ->{}
                is Failed ->{}
                Loading -> {}
                Success -> {
//                    ToastUtils.showLong(context,"成功")
                    adapter.setData(mViewModel.merchantAList)
                }
            }
        }
    }


    fun initAdapter(){
        mBinding.rv.layoutManager= LinearLayoutManager(context)
        mBinding.rv.adapter=adapter
    }


}